package com.chouxiaozi.wechatpublic.service;

import com.chouxiaozi.wechatpublic.entity.WechatGroup;
import com.chouxiaozi.wechatpublic.service.wechat.IWechatCustomMenuService;
import com.chouxiaozi.wechatpublic.service.wechat.IWechatKeywordReplyService;
import com.chouxiaozi.wechatpublic.service.wechat.IWechatSubscribeReplyService;
import com.chouxiaozi.wechatpublic.vo.WechatCustomMenuVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.weixin4j.Weixin;
import org.weixin4j.component.MenuComponent;
import org.weixin4j.model.menu.ClickButton;
import org.weixin4j.model.menu.Menu;
import org.weixin4j.model.menu.SingleButton;
import org.weixin4j.model.menu.ViewButton;

import java.util.ArrayList;
import java.util.List;

@Service
public class InitializeService {

	private static Logger logger = LoggerFactory.getLogger(InitializeService.class);

	@Autowired
	private IWechatGroupService wechatGroupService;

	@Autowired
	private IWechatCustomMenuService wechatCustomMenuService;

	@Autowired
	private IWechatKeywordReplyService wechatKeywordReplyService;

	@Autowired
	private IWechatSubscribeReplyService wechatSubscribeReplyService;

	public void initalizeWechat(Integer groupId) throws Exception {


			//1.初始化菜单 -并创建到微信服务器
			WechatGroup group =  wechatGroupService.getById(groupId);

			//wechatCustomMenuService.initMenu(group);

			//生效同步到微信
			effectWxMenu(group);

			//2.初始化关键字回复
			//wechatKeywordReplyService.initReply(group);

			//3.初始化首次回复
			//wechatSubscribeReplyService.initReply(group);

	}

	//生效微信菜单
	public void effectWxMenu(WechatGroup group) throws Exception {
		//查询菜单
		List<WechatCustomMenuVo>  menusLsit = wechatCustomMenuService.getMenuByGroupCode(group.getCode());
		List<SingleButton> buttons = new ArrayList<SingleButton>();

		for(WechatCustomMenuVo pMenu : menusLsit) {

			String menuType = pMenu.getMenuType();
			if(menuType.equals("1")) {  //1标签

				SingleButton singleButton = new SingleButton(pMenu.getMenuName());

				List<WechatCustomMenuVo>  sonMenus = pMenu.getChList();

				List<SingleButton> subButtons = new ArrayList<SingleButton>();
				for(WechatCustomMenuVo sonMenu:sonMenus) {
					String sonMenuType = sonMenu.getMenuType();
					if(sonMenuType.equals("1")){ //1标签

					}else if(sonMenuType.equals("2")){ //2命令菜单
						subButtons.add(new ClickButton(sonMenu.getMenuName(),sonMenu.getMenuKey()));
					}else if(sonMenuType.equals("3")){  //3链接菜单
						String url = sonMenu.getUrl();
						//url = url.replaceAll("\\[serverurl\\]", Configuration.getProperty("weixin4j.interface.url"));
						url = url.replaceAll("\\[serverurl\\]", "http://www.baidu.com");
						url = url.replaceAll("\\[groupcode\\]", group.getCode());

						if(sonMenu.getIsGrant()==1){
							//菜单都需要微信授权
							//url = WinxinHelper.getInstance().getOAuth2UserUrl(url,group.getWxGzhAppid());
						}

						subButtons.add(new ViewButton(sonMenu.getMenuName(), url));
					}
				}
				// 设置子菜单
				singleButton.setSubButton(subButtons);
				buttons.add(singleButton);

			}else if(menuType.equals("2")) {   //2命令菜单

				buttons.add(new ClickButton(pMenu.getMenuName(), pMenu.getMenuKey()));

			}else if(menuType.equals("3")) {  //3链接菜单
				String url = pMenu.getUrl();
				url = url.replaceAll("\\[serverurl\\]", "http://www.baidu.com");
				url = url.replaceAll("\\[groupcode\\]", group.getCode());
//				url = url.replaceAll("\\[serverurl\\]", Configuration.getProperty("weixin4j.interface.url"));
//				url = url.replaceAll("\\[groupcode\\]", group.getCode());

				if(pMenu.getIsGrant()==1){
					//菜单需要微信授权
					//url = WinxinHelper.getInstance().getOAuth2UserUrl(url,group.getWxGzhAppid());
				}

				buttons.add(new ViewButton(pMenu.getMenuName(), url));
			}

		}
		//Weixin weixin = WinxinHelper.getInstance().getWeixin(group.getWxGzhAppid(),group.getWxGzhSecret(),group.getWxGzhTousername());
		Weixin weixin = new Weixin(group.getWxGzhAppid(),group.getWxGzhSecret());
		Menu menu = new Menu();
		menu.setButton(buttons);
		logger.info(menu.toJSONObject().toString());
		// 创建自定义菜单
		MenuComponent menuComponent = weixin.menu();
		menuComponent.create(menu);
		logger.info(group.getWxGzhTousername()+"创建菜单完成!");
	}





}
